package com.tidc.seaservermodel.exception;


import com.tidc.seaservermodel.entity.Fres;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020-3-28 22:51
 * @description：异常控制器
 * @modified By：
 * @version:
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
  

    @ExceptionHandler(TidcFileException.class)
    @ResponseBody
    public Fres<Integer> TidcFileException(TidcFileException exception) {
        log.error(exception.getMessage(), exception);
        return Fres.badRequestParent(405, exception.getMessage());
    }

    @ExceptionHandler(TidcNulException.class)
    @ResponseBody
    public Fres<Integer> TidcNulException(TidcNulException exception) {
        log.error(exception.getMessage(), exception);
        return Fres.badRequestParent(445, exception.getMessage());
    }

    @ExceptionHandler(TidcAuthorityException.class)
    @ResponseBody
    public Fres<Integer> TidcAuthorityException(TidcAuthorityException exception) {
        log.error(exception.getMessage(), exception);
        return Fres.unAuthentication(401, exception.getMessage()!=null?exception.getMessage():"你的权限不足");
    }

    @ExceptionHandler(SQLException.class)
    public Fres<Integer> sqlException(SQLException e) {
        log.error(e.getMessage(), e);
        Fres<Integer> fres = new Fres<>();
        fres.getMeta().setStatus(406).setMessage(e.getMessage());
        return fres;
    }

    @ExceptionHandler(TidcUnLogin.class)
    @ResponseBody
    public Fres<Integer> TidcUnLoginException(TidcUnLogin exception) {
        log.error(exception.getMessage(), exception);
        return Fres.unLogin(402, exception.getMessage());
    }

    @ExceptionHandler(TidcBadParent.class)
    @ResponseBody
    public Fres<Integer> TidcBadParentException(TidcBadParent exception) {
        log.error(exception.getMessage(), exception);
        return Fres.unAuthentication(400, exception.getMessage());
    }
    @ExceptionHandler(TidcQuartzException.class)
    @ResponseBody
    public Fres<Integer> TidcQuartzException(TidcQuartzException exception) {
        log.error(exception.getMessage(), exception);
        return Fres.unAuthentication(406, exception.getMessage());
    }
    @ExceptionHandler(TidcRestException.class)
    @ResponseBody
    public Fres<Integer> TidcRestException(TidcRestException exception) {
        log.error(exception.getMessage(), exception);
        return Fres.unAuthentication(500, exception.getMessage());
    }

    //校验逻辑抛出
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Fres<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException exception) {
        Map<String, String> errors = new HashMap<>();
        exception.getBindingResult().getAllErrors().forEach((error) -> {
            String fieldName = ((FieldError) error).getField();
            String errorMessage = error.getDefaultMessage();
            errors.put(fieldName, errorMessage);
        });
        Fres<Map<String, String>> fres = Fres.badRequestParent(errors, "参数错误");
        return fres;
    }
    //HttpRequestMethodNotSupportedException 请求方式异常

    /**
     * 处理，实际为捕捉全局异常
     *
     * @param exception 全局异常
     * @return 具体异常信息 后面那个是500报错内容是服务器内部异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Fres<String> Exception(Exception exception) {
        //当未知异常发生时，将信息弹出堆栈
        log.error(exception.getMessage(), exception);
        exception.printStackTrace();
        return Fres.ErrorFre("请求异常");
    }
}
